import { Menu } from '@/business/menu/entities/menu.entity';
import { User } from '@/business/user/entities/user.entity';
import { CommonEntity } from '@/common/common-entity';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Column, Entity, JoinTable, ManyToMany } from 'typeorm';

@Entity('role')
export class Role extends CommonEntity {
  @ApiPropertyOptional({ description: '角色名称' })
  @Column({
    type: 'varchar',
    unique: true,
    comment: '角色名称',
    length: 48
  })
  name: string;

  @ApiPropertyOptional({ description: '角色权限标识' })
  @Column({
    type: 'varchar',
    unique: true,
    nullable: false,
    comment: '角色权限标识'
  })
  mark: string;

  @ApiPropertyOptional({ description: '角色启用状态, 0禁用,1启用' })
  @Column({
    type: 'tinyint',
    nullable: true,
    width: 1,
    comment: '角色启用状态, 0禁用,1启用'
  })
  status: number;

  @ApiPropertyOptional({ description: '角色描述' })
  @Column({
    type: 'varchar',
    nullable: true,
    comment: '角色描述',
    length: 255
  })
  desc: string | null;

  @ApiPropertyOptional({ description: '角色拥有的人员', type: [User] })
  @ManyToMany(() => User, (manager) => manager.roles)
  users: User[];

  @ApiPropertyOptional({ description: '角色拥有的菜单', type: [Menu] })
  @ManyToMany(() => Menu, (menu) => menu.roles, {
    createForeignKeyConstraints: false,
    cascade: true
  })
  @JoinTable({ name: 'role_menu' })
  menus: Menu[];
}
